<--- %%NOBANNER%% --> replace.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
|  Replace a substring in a macro string with different substrings; |
|  For Example: Use of translate: replace only one character a time; |
|               /tranwrd: replace a substring or a character;        |
|               the output is my-time-text.                          |
|  Distinction:                                                      |
|                  translate(sting, with, replace);                  |
|                  tranwrd(string, replace, with);                   |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Argument                                                           |
|    string: the input string;                                       |
|    replace: the substring you want to replace;                     |
|    with: the new substring you want to replace to;                 |
|    return: if you want to return the result,                       |
|           T - return the result as string;                         |
|           F - do not return the string, just write it in the log;  |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
|  Example: %replace("This is a sample", "is", "aw");                |
|           %replace('This is an example', %str(%'), %str(%"));      |
|  Usage:   %replace(string, replace, with, return=F);               |
\-------------------<-- End of Files Created-->---------------------*/
%macro replace(string, replace, with, return=F);
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  8-27-2001 10:11pm;                |
| Modified: 12-25-2001 9:36am;                |
| Purpose:  Replace a substring in a text with|
|           different substring;              |
\--------------------------------------------*/
%local string oristr;
%let oristr=&string;
%let string=%sysfunc(tranwrd(&string,&replace,&with));
%if (not %index(&string, %str(%"))) %then %do;
   %put --> Note: The string is changed to "&string". ; 
%end;
%else %do;
   %put --> Note: The string is changed to &string. ; 
%end;
%if (%index(%upcase(&return),T)) %then %do;
   &string
%end;
%mend replace;